package com.znyj.cloud.chain.util;

import com.alibaba.fastjson.JSONObject;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by gaoliwei02 on 17/6/9.
 */
public class CheckUtil {
//    public static boolean checkIsValid(long num) {
//        if (num >= 0) {
//            return true;
//        }
//        return false;
//    }
//
//    public static boolean checkIsValid(String num) {
//        if (num != null) {
//            return true;
//        }
//        return false;
//    }
    //long <0
    //String "" null
    public static boolean IsValid(Object... vaule) {
        for (Object p : vaule) {
            if (p instanceof Long) {
                if ((long) p < 0) return false;
            } else if (p instanceof String) {
                if ((String) p == null) return false;
                else if (((String) p).equals("")) return false;
                //else if (((String) p).equals("0")) return false;
            } else {
                return false;
            }
        }
        return true;
    }

    public static String monitorReport(int errorCode, String msg, long num) {
        JSONObject json = new JSONObject();
        Timestamp now = new Timestamp(System.currentTimeMillis());
        json.put("monitorCode", errorCode);
        json.put("now", now);
        json.put("msg", msg);
        json.put("count", num);
        return json.toString();
    }

    public static String dataReport(String type, String data) {
        JSONObject json = new JSONObject();
        json.put(type,JSONObject.parse(data));
        return json.toString();
    }

    public static String timeTakeReport(Date begin, String deviceId) {
        JSONObject json = new JSONObject();
        json.put("timeTaking", deviceId);
        Date now = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(begin);

        json.put("begin", dateString);
        json.put("delta", now.getTime()-begin.getTime());
        return json.toString();
    }
}
